package servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import entity.Household;
import service.HouseholdrService;
import service.impl.HouseholdServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author pengrui
 * @version 1.0
 * @date 2021/9/9 20:18
 * @Description //更新household中的数据
 */

@WebServlet("/AddHouseholdServlet")
public class AddHouseholdServlet extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");//！！！这里注意必须写在第一位，utf-8大小写都可以
        String name=request.getParameter("name");
        String sex=request.getParameter("sex");
        String phoneNumber=request.getParameter("phoneNumber");
        String company=request.getParameter("company");
        String community=request.getParameter("community");
        String location=request.getParameter("location");

        Household household=new Household(name, sex, phoneNumber, company, community, location);
        HouseholdrService householdrService=new HouseholdServiceImpl();

        PrintWriter printWriter=response.getWriter();
        printWriter.print(new ObjectMapper().writeValueAsString(householdrService.saveHousehold(household)));

    }


}
